package com.youths.controller;

import com.google.gson.Gson;
import com.youths.domain.LabelConsumer;
import com.youths.domain.Slice;
import com.youths.domain.YouthInfo;
import com.youths.response.Result;
import com.youths.service.SliceService;
import com.youths.service.YouthInfoService;
import com.youths.util.EnDecoderUtil;
import com.youths.util.HttpAPIUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.security.spec.EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: KingRainGrey
 * Date: 2020/6/27
 */
@Controller
@RequestMapping(value = "/")
@Slf4j
public class SliceController {
    @Autowired
    YouthInfoService youthInfoService;

    @Autowired
    SliceService sliceService;

    @Autowired
    HttpAPIUtil httpAPIUtil;

    @Value("${des.key}")
    String desKey;


    @GetMapping(value = "upload")
    public String upload(HttpSession session, Model model) {
        try {
            LabelConsumer consumer = (LabelConsumer) session.getAttribute("user");
            if (consumer != null) {
                int consumerId = consumer.getConsumerId();
                List<YouthInfo> youthInfos = youthInfoService.getInfoList(consumerId);
                model.addAttribute("youths", youthInfos);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return "analysis/upload";
    }

    @PostMapping(value = "saveSlice", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String saveSlice(@RequestParam("boneDicomPath") String boneDicomPath,
                            @RequestParam("boneImgPath") String boneImgPath,
                            @RequestParam("httpPath") String httpPath,
                            @RequestParam("height") Float height,
                            @RequestParam("weight") Float weight,
                            @RequestParam("physicalTime") String physicalTime,
                            @RequestParam("youthId") Integer youthId) {
        Gson gson = new Gson();
        Map<String, Object> resMap = new HashMap<>();
        try {
            log.info("number:" + height);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            int result = sliceService.addSlice(boneDicomPath, boneImgPath, httpPath, height, weight, sdf.parse(physicalTime), youthId, 0);
            if (result > 0) {
                resMap.put("res", 1);
            }else {
                resMap.put("res", 0);
            }
        }catch (Exception e){
            e.printStackTrace();
            resMap.put("res", 0);
        }
        return gson.toJson(resMap);
    }

    @RequestMapping(value = "/file/uploadDicom")
    //@PostMapping(value = "uploadDicom", consumes = "multipart/form-data")
    @ResponseBody
    public String uploadDicom(@RequestParam("file") MultipartFile file) {
        Gson gson = new Gson();
        Map<String, Object> resMap = new HashMap<>();
        try {
            int sliceId = sliceService.uploadDicomWithSave(file);
            if (sliceId > 0) {
                Slice slice = sliceService.query(sliceId);
                if (slice.getPhysicalTime() == null) {
                    resMap.put("date", 0);
                }else {
                    resMap.put("date", slice.getPhysicalTime());
                }
                resMap.put("code", 1);
                byte[] encode_bytes = EnDecoderUtil.DESEncrypt(desKey, sliceId + "");
                String sliceStr = Base64.getEncoder().encodeToString(encode_bytes);
                resMap.put("sliceId", sliceStr);
                resMap.put("url", slice.getHttpPath());
            }else {
                resMap.put("code", 0);
            }
        }catch (Exception e) {
            resMap.put("code", 0);
            e.printStackTrace();
        }

        return gson.toJson(resMap);
    }

    @GetMapping("/back/getSliceList")
    @ResponseBody
    public Result getSliceList(@RequestParam("pageNum") Integer pageNum,
                               @RequestParam("slicingId") Integer slicingId,
                               @RequestParam("date1") String date1,
                               @RequestParam("date2") String date2,
                               @RequestParam("card") String card){
        return Result.success(sliceService.getSliceList(pageNum,slicingId,date1,date2,card));
    }

}
